﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using EBAYVN.DOMAIN;
using EBAYVN.Util;
using System.Configuration;

namespace EBAYVN.DAO
{
    public class FunctionDao
    {
        public static readonly string Connectionstring = ConfigurationManager.AppSettings[Constants.SETTINGS_CONNECTION];
        private readonly EBAYVNDOMAINDataContext _db;

        /// <summary>
        /// constructor
        /// </summary>
        public FunctionDao()
        {
            _db = new EBAYVNDOMAINDataContext(Connectionstring);
        }

        /// <summary>
        /// Get function by Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BiddingFunction Get(int id)
        {
            return _db.BiddingFunctions.SingleOrDefault(g => g.Id.Equals(id)
                                                         && g.Deleted.Equals(false));
        }

        public BiddingFunction GetFromURL(string urlPath)
        {
            return _db.BiddingFunctions.Where(w => w.Url.ToLower().Contains(urlPath.ToLower())).Distinct().SingleOrDefault();
        }

        /*
        public BiddingFunction GetFromURL(string urlPath)
        {
            urlPath = urlPath.Substring("http://".Length);
            urlPath = urlPath.Substring(urlPath.IndexOf('/') + 1);
            urlPath = urlPath.Substring(0, urlPath.IndexOf(".aspx", System.StringComparison.OrdinalIgnoreCase) + ".aspx".Length);
            return _db.BiddingFunctions.Where(w => w.Url.ToLower().Contains(urlPath.ToLower())).Distinct().SingleOrDefault();
        }
        */

        public string GetPath(int pID) { return GetPath(pID, 0); }
        
        protected string GetPath(int pID, int overFlow)
        {
            var res = "";
            if (overFlow < 10)
            {
                var curID = _db.BiddingFunctions.Where(w => w.Id == pID).Distinct().SingleOrDefault();
                if (curID == null) return "";
                if (curID.Parent > 0) res = GetPath(curID.Parent, overFlow + 1);
                res += (res != "" ? " > " : "") + curID.Name;
            }
            return res;
        }
        
        
        public string GetModuleName(int pID)
        {
            var curID = _db.BiddingFunctions.Where(w => w.Id == pID).Distinct().SingleOrDefault();
            if (curID == null) return "";

            return curID.Parent > 0 ? GetModuleName(curID.Parent) : curID.Name;
        }

        public string GetPageTitle(int id)
        {
            var curID = _db.BiddingFunctions.Where(w => w.Id == id).Distinct().SingleOrDefault();
            if (curID == null) return "";

            if (curID.Parent > 0)
            {
                var tmp = GetPageTitle(curID.Parent);
                if(tmp != "") return GetPageTitle(curID.Parent) + " > " + curID.Name;
                return curID.Name;
            }
          
            return "";
        }

        public string GetFunctionFullName(BiddingFunction function)
        {
            if (function.Parent > 0)
            {
                var tmp = GetPageTitle(function.Parent);
                if (tmp != "") return GetPageTitle(function.Parent) + " > " + function.Name;
                return function.Name;
            }

            return "";
        }

        public List<ListItem> GetManagedList()
        {
            var result = new List<ListItem>();
            var text = "";
            var value = "";

            var list = _db.BiddingFunctions.Where(f => f.Parent != 0 && f.Deleted.Equals(false)).ToList();

            foreach (var BiddingFunction in list)
            {
                value = BiddingFunction.Id.ToString();
                text = GetModuleName(BiddingFunction.Id) + " > " + GetFunctionFullName(BiddingFunction);
                result.Add(new ListItem(text, value));
            }

            return result;


        }

        /// <summary>
        /// Get function list
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<BiddingFunction> GetList(int id)
        {
            return _db.BiddingFunctions.Where(g => g.Parent.Equals(id) &&
                                               g.Deleted.Equals(false)).
                                               OrderBy(f => f.Order).
                                               ToList();
        }

        /// <summary>
        /// Get function list by parent id and logged in username
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public List<BiddingFunction> GetList(int id, string userName)
        {
            return _db.GetFunctionList(id, userName).ToList();
        }
    }
}
